package com.lottery.service.pc28.lobbyService.impl;

import com.lottery.dao.pc28.lobbyDao.BannedWordsDao;
import com.lottery.dto.PageDto;
import com.lottery.dto.ResultDto;
import com.lottery.entity.BannedWords;
import com.lottery.service.pc28.lobbyService.BannedWordsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class BannedWordsServiceImpl implements BannedWordsService {
	@Autowired
	private BannedWordsDao bannedWordsDao;
	

	/**
	 * 校验输入信息
	 * @param param
	 * @return
	 */
	public boolean checkParam(String param) {
		List<BannedWords> selectAllBannedWords = bannedWordsDao.selectAllBannedWords();
		if(selectAllBannedWords==null||selectAllBannedWords.size()==0){
			return true;
		}
		boolean flag =  true;
		for (BannedWords bannedWords : selectAllBannedWords) {
			if(param.contains(bannedWords.getWordsInfo())){
				flag=false;
			}
		}
		return flag;
	}
	
	
	/**
	 * 分页查询信息
	 * 
	 * @return
	 */
	public ResultDto<PageDto> selectAllBannedWords(int pageNow) {
		ResultDto<PageDto> resultDto = new ResultDto<PageDto>();
		PageDto pageDto = new PageDto();
		int pageSize = 10;
		int totalCount = 0;
		try {
			totalCount = bannedWordsDao.selectBannedWordsCount();
		} catch (Exception e) {
			resultDto.setSuccess(false);
			return resultDto;
		}

		int totalPageCount = (totalCount + pageSize - 1) / pageSize;
		int startPage = (pageNow - 1) * pageSize;
		pageDto.setPageSize(pageSize);
		pageDto.setPageNow(pageNow);
		pageDto.setstartPage(startPage);
		pageDto.setTotalCount(totalCount);
		pageDto.setTotalPageCount(totalPageCount);
		resultDto.setData(pageDto);

		try {
			List<BannedWords> selectAllBannedWords = bannedWordsDao.selectPageBannedWords(pageDto);
			for (int i = 0; i < selectAllBannedWords.size(); i++) {
				String  bannedWordsInfo = selectAllBannedWords.get(i).getWordsInfo();
				if(bannedWordsInfo.length()>30){
					selectAllBannedWords.get(i).setWordsInfo(bannedWordsInfo.substring(0, 30)+"...");
				}
			}
			resultDto.setList(selectAllBannedWords);
			resultDto.setSuccess(true);
		} catch (Exception e) {
			resultDto.setSuccess(false);
		}
		return resultDto;
	}
	
	/**
	 * 后台管理员插入铭感词汇
	 * 
	 * @param BannedWordsInfo
	 *            
	 * @return
	 */
	public ResultDto<BannedWords> adminInsertBannedWords(String bannedWordsInfo) {
		ResultDto<BannedWords> resultDto = new ResultDto<BannedWords>();
		BannedWords bannedWords = new BannedWords();
		bannedWords.setWordsDate(new  Date());
		bannedWords.setIsDelete(0); // 管理员发布默认 0
		bannedWords.setWordsInfo(bannedWordsInfo);
		try {
			bannedWordsDao.insertBannedWords(bannedWords);
			resultDto.setSuccess(true);
		} catch (Exception e) {
			resultDto.setSuccess(false);
		}
		return resultDto;
	}
	
	/**
	 * 删除信息
	 * 
	 * @param id
	 * @return
	 */
	public ResultDto<BannedWords> deleteBannedWords(int id) {
		ResultDto<BannedWords> resultDto = new ResultDto<BannedWords>();
		BannedWords bannedWords = new BannedWords();
		bannedWords.setId(id);
		bannedWords.setIsDelete(1);
		try {
			bannedWordsDao.updateBannedWords(bannedWords);
			resultDto.setSuccess(true);
		} catch (Exception e) {
			resultDto.setSuccess(false);
		}
		return resultDto;
	}
	
	/**
	 * 修改信息
	 * 
	 * @param id
	 * @param noticeInfo
	 * @return
	 */
	public ResultDto<BannedWords> editBannedWords(int id, String bannedWordsInfo) {
		ResultDto<BannedWords> resultDto = new ResultDto<BannedWords>();
		BannedWords bannedWords = new BannedWords();
		bannedWords.setWordsDate(new  Date());
		bannedWords.setIsDelete(0); // 管理员发布默认 0
		bannedWords.setWordsInfo(bannedWordsInfo);

		BannedWords bannedWords2 = new BannedWords();
		bannedWords2.setId(id);
		bannedWords2.setIsDelete(1);

		try {
			bannedWordsDao.insertBannedWords(bannedWords);
			bannedWordsDao.updateBannedWords(bannedWords2);
			resultDto.setSuccess(true);
		} catch (Exception e) {
			resultDto.setSuccess(false);
		}
		return resultDto;
	}
	
	/**
	 * 根据id查询记录
	 * @param id
	 * @return
	 */
	public ResultDto<BannedWords> selectOneBannedWords(int id) {
		ResultDto<BannedWords> resultDto = new ResultDto<BannedWords>();
		try {
			BannedWords selectBannedWordsById = bannedWordsDao.selectBannedWordsById(id);
			resultDto.setData(selectBannedWordsById);
			resultDto.setSuccess(true);
		} catch (Exception e) {
			resultDto.setSuccess(false);
		}
		return resultDto;
	}

}
